home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2 - Developers' Solutions / Delphi 2 Developers' Solutions.iso / dds / sharware / dynarray / multidim.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-05-26  |  3.2 KB  |  108 lines

  1. {+------------------------------------------------------------
  2.  | Unit Multidim
  3.  |
  4.  | Version: 1.0  Created: 05/26/96, 16:55:35
  5.  |               Last Modified: 05/26/96, 16:55:35
  6.  | Author : P. Below
  7.  | Project: Array classes
  8.  | Description:
  9.  |   This is a brief sketch of how multidimensional arrays
  10.  |   could be implemented based on the one-dimensional arrays
  11.  |   class in Arrays.Pas. This class is bare of any error
  12.  |   checking and would need quite a bit of work in this
  13.  |   department!
  14.  +------------------------------------------------------------}
  15. Unit Multidim;
  16. Interface
  17.  
  18. Uses Classes, Arrays;
  19.  
  20. Type
  21.   TMultiDimArray = Class(TPersistent)
  22.   private
  23.     FStorage: TBaseArray;
  24.     FBounds : TCardinalArray;
  25.  
  26.     Function  GetMaxIndex( dimension: Cardinal ): Cardinal;
  27.   public
  28.     Constructor Create( Const numItems: Array of Const;
  29.                         datasize: Cardinal ); virtual;
  30.     Destructor Destroy; override;
  31.     Function OneDIndex(Const Indices: Array of Const): Cardinal;
  32.     Procedure PutItem( Const Indices: Array of Const; Var data );
  33.     Procedure GetItem( Const Indices: Array of Const; Var data );
  34.  
  35.     property MaxIndex[ dimension: Cardinal ]: Cardinal 
  36.       read GetMaxIndex;
  37.     property Storage: TBaseArray read FStorage;
  38.   End;
  39.  
  40. Implementation
  41.  
  42. Uses SysUtils;
  43.  
  44. {+---------------------------
  45.  | Methods of TMultiDimArray 
  46.  +--------------------------}
  47. Function  TMultiDimArray.GetMaxIndex( dimension: Cardinal ): Cardinal;
  48.   Begin
  49.     Result := Pred(FBounds[Pred(dimension)]);
  50.   End; { TMultiDimArray.GetMaxIndex }
  51.   
  52. Constructor TMultiDimArray.Create( Const numItems: Array of Const;
  53.                     datasize: Cardinal ); 
  54.   Var
  55.     i: Cardinal;
  56.     total: Cardinal;
  57.   Begin
  58.     inherited Create;
  59.     FBounds := TCardinalArray.Create( Succ(High(numItems)), 0);
  60.     total := 1;
  61.     For i:= 0 To High(numItems) Do Begin
  62.       FBounds[i] := numItems[i].VInteger;
  63.       total := total * numItems[i].VInteger;
  64.     End; { For }
  65.  
  66.     FStorage := TBaseArray.Create( total, datasize );
  67.   End; { TMultiDimArray.Create }
  68.   
  69. Destructor TMultiDimArray.Destroy; 
  70.   Begin
  71.     FStorage.Free;
  72.     FBounds.Free;
  73.     inherited Destroy;
  74.   End; { TMultiDimArray.Destroy }
  75.   
  76. Function TMultiDimArray.OneDIndex(Const Indices: Array of Const): Cardinal;
  77.   Var
  78.     i: Cardinal;
  79.     dimsize: Cardinal;
  80.   Begin
  81.     If High(Indices) = FBounds.MaxIndex Then Begin
  82.       Result := 0;
  83.       dimsize := 1;
  84.       For i:= High(Indices)downto 0 Do Begin
  85.         Result := Result + Indices[i].VInteger * dimsize;
  86.         dimsize := dimsize * FBounds[i];
  87.       End; { For }
  88.     End { If }
  89.     Else
  90.       raise ERangeError.Create(
  91.              'Number of indices does not match definition!');
  92.   End; { TMultiDimArray.OneDIndex }
  93.   
  94. Procedure TMultiDimArray.PutItem( Const Indices: Array of Const; Var data );
  95.   Begin
  96.     FStorage.PutItem( OneDIndex(Indices), data );
  97.   End; { TMultiDimArray.PutItem }
  98.   
  99. Procedure TMultiDimArray.GetItem( Const Indices: Array of Const; Var data );
  100.   Begin
  101.     FStorage.GetItem( OneDIndex(Indices), data );
  102.   End; { TMultiDimArray.GetItem }
  103.   
  104.  
  105. Initialization
  106. End. { Unit Multidim }
  107.  
  108.